﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Toolkit.Trigger
{
    /// <summary>
    /// 点击的时候将文本内容复制到剪贴板
    /// </summary>
    public class CopyWhenClick : TriggerBase<TextBlock>
    {
        #region 保护方法

        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.Cursor = Cursors.Hand;
            this.AssociatedObject.MouseLeftButtonDown += Copy;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            this.AssociatedObject.Cursor = Cursors.Arrow;
            this.AssociatedObject.MouseLeftButtonDown -= Copy;
        }

        #endregion

        /// <summary>
        /// 清空信息
        /// </summary>
        /// <param name="sender">触发对象</param>
        /// <param name="e">监视对象</param>
        void Copy(object sender, EventArgs e)
        {
            
            try
            {
                string text = ((TextBlock)sender).Text;
                Clipboard.SetText(text);
                if (CopyComplete != null)
                CopyComplete(sender,new CopyCompleteArgs(true)); 
            }
            catch  
            {
                if(CopyComplete!=null)
                CopyComplete(sender,new CopyCompleteArgs(false));   
            }
           
        }

        public event EventHandler<CopyCompleteArgs> CopyComplete;

    }

    public class CopyCompleteArgs : EventArgs
    {
        public bool Success { set; get; }

        public CopyCompleteArgs(bool s)
        {
            Success = s;
        }
    }
}
